{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(matrix([[ 0.24838344, -1.274323  ,  1.        ],\n",
       "         [ 0.13936921, -0.398576  ,  1.        ],\n",
       "         [ 0.30242631, -1.238267  ,  1.        ],\n",
       "         [ 0.85829997, -3.583607  ,  1.        ],\n",
       "         [ 0.45641567, -1.745203  ,  1.        ]]),\n",
       " matrix([[-24.897896],\n",
       "         [-23.759634],\n",
       "         [-26.591775],\n",
       "         [-32.467744],\n",
       "         [-33.943239]]))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "#加载数据\n",
    "def load_data():\n",
    "    with open('多变量线性数据.txt') as fr:\n",
    "        lines = fr.readlines()\n",
    "\n",
    "    #在x当中多加一列,全部为0,用来计算b\n",
    "    x = np.ones((len(lines), 3), dtype=float)\n",
    "    y = np.empty((len(lines), 1), dtype=float)\n",
    "\n",
    "    for i in range(len(lines)):\n",
    "        line = lines[i].strip().split(',')\n",
    "        x[i, :2] = line[:2]\n",
    "        y[i] = line[2]\n",
    "\n",
    "    #这里给他填充点随机数,不然会导致x*x是奇异矩阵,无法求逆\n",
    "    x[:, 0] = np.random.rand(len(x))\n",
    "\n",
    "    #转矩阵\n",
    "    return np.mat(x), np.mat(y)\n",
    "\n",
    "\n",
    "x, y = load_data()\n",
    "x[:5], y[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[0.],\n",
       "        [1.],\n",
       "        [0.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#常量\n",
    "N, M = x.shape\n",
    "#现在的w当中包括了w和b\n",
    "w = np.mat([[0], [1], [0]], dtype=float)\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.274323"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#预测函数\n",
    "def predict(x):\n",
    "    return (x * w)[0, 0]\n",
    "\n",
    "\n",
    "predict(x[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "138591.69141975886"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#求loss,就是简单的做差求平方和\n",
    "def get_loss():\n",
    "    loss = (y - x * w).T * (y - x * w)\n",
    "    return loss[0, 0]\n",
    "\n",
    "\n",
    "get_loss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ -0.7812822 ],\n",
       "        [  2.51906855],\n",
       "        [-22.7090768 ]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#这个公式是从书上抄的,没推算过\n",
    "def get_w():\n",
    "    xx = x.T * x\n",
    "    \n",
    "    #求内积的行列式值,如果是奇异矩阵就不能求逆,就不能用这种方法了\n",
    "    if np.linalg.det(xx) == 0:\n",
    "        raise Exception('奇异矩阵,无法求逆')\n",
    "\n",
    "    w = xx.I * x.T * y\n",
    "    return w\n",
    "\n",
    "\n",
    "w = get_w()\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-26.1132413470429 [[-24.897896]]\n",
      "-23.822003752572375 [[-23.759634]]\n",
      "-26.06463654631646 [[-26.591775]]\n",
      "-32.407002970097956 [[-32.467744]]\n",
      "-27.461952225604232 [[-33.943239]]\n",
      "-26.74394184810255 [[-22.886769]]\n",
      "-27.29095930303123 [[-32.595698]]\n",
      "-31.840712988806395 [[-32.961657]]\n",
      "-31.576759485513318 [[-32.260963]]\n",
      "-24.520580232878675 [[-22.91686]]\n",
      "-22.97869942143672 [[-23.224347]]\n",
      "-29.859268102638964 [[-26.761884]]\n",
      "-26.97073575821918 [[-26.83519]]\n",
      "-26.534079589784852 [[-28.29683]]\n",
      "-27.247263510175493 [[-22.700174]]\n",
      "-26.213294184611254 [[-25.399424]]\n",
      "-24.81391009539039 [[-26.365]]\n",
      "-28.667868575016506 [[-24.191492]]\n",
      "-30.24742268100096 [[-29.502944]]\n",
      "-28.796875278544203 [[-24.260812]]\n",
      "-30.765669822811937 [[-27.233203]]\n",
      "-27.622491936493894 [[-31.615989]]\n",
      "-27.95445382934521 [[-28.340193]]\n",
      "-26.895857009029587 [[-24.714052]]\n",
      "-31.994535904586865 [[-32.626654]]\n",
      "-25.87372494635575 [[-24.617518]]\n",
      "-24.38712226792069 [[-27.533744]]\n",
      "-28.330463790984318 [[-29.187444]]\n",
      "-25.289282517388326 [[-22.808779]]\n",
      "-27.424712416342977 [[-24.02964]]\n",
      "-28.04373818631622 [[-25.882721]]\n",
      "-25.362205046389192 [[-20.574579]]\n",
      "-30.44330745110499 [[-37.785277]]\n",
      "-26.71899192989796 [[-29.384143]]\n",
      "-25.991956739899628 [[-28.87989]]\n",
      "-27.5808329150061 [[-29.811187]]\n",
      "-27.124956772645596 [[-27.315989]]\n",
      "-24.964570835688505 [[-22.345454]]\n",
      "-24.481700873817175 [[-23.006784]]\n",
      "-27.830338334141405 [[-27.018666]]\n",
      "-22.157560340850736 [[-29.114606]]\n",
      "-27.14482010295002 [[-24.050712]]\n",
      "-28.159342211989813 [[-28.965701]]\n",
      "-28.08446922828708 [[-28.807204]]\n",
      "-25.75249087484834 [[-27.172519]]\n",
      "-30.03151986866575 [[-24.526498]]\n",
      "-32.495035611181514 [[-31.034457]]\n",
      "-33.96856168830955 [[-35.131266]]\n",
      "-28.934968318314816 [[-26.359204]]\n",
      "-25.024999848166107 [[-25.152534]]\n",
      "-32.83422146742702 [[-36.60391]]\n",
      "-28.07547829895617 [[-26.764776]]\n",
      "-28.909341798200455 [[-29.77079]]\n",
      "-27.969981155467803 [[-28.790259]]\n",
      "-26.44783290523614 [[-30.883915]]\n",
      "-28.292716471772085 [[-24.488038]]\n",
      "-28.3392645543588 [[-25.877444]]\n",
      "-26.565947470158356 [[-22.660873]]\n",
      "-25.004879720944075 [[-22.095426]]\n",
      "-26.7333200940444 [[-23.373639]]\n",
      "-30.68714270263156 [[-31.096975]]\n",
      "-31.128476232990145 [[-31.193845]]\n",
      "-30.218255448924765 [[-34.145095]]\n",
      "-25.97032413645586 [[-32.23144]]\n",
      "-29.373348532721774 [[-30.990845]]\n",
      "-29.74701391747532 [[-26.665352]]\n",
      "-29.023711501917557 [[-30.214546]]\n",
      "-24.88457567172679 [[-27.82455]]\n",
      "-27.37304114347864 [[-28.595442]]\n",
      "-32.482971569301554 [[-36.024308]]\n",
      "-25.940474178458604 [[-28.867871]]\n",
      "-28.233398814251334 [[-32.466004]]\n",
      "-29.614857485672054 [[-28.362192]]\n",
      "-25.589158481506857 [[-29.193895]]\n",
      "-29.68006492372407 [[-28.363757]]\n",
      "-28.15496474282201 [[-27.181018]]\n",
      "-28.76661620115043 [[-24.557624]]\n",
      "-29.29315239473179 [[-32.281904]]\n",
      "-29.70138700155795 [[-30.176946]]\n",
      "-26.410109633577807 [[-25.772576]]\n",
      "-32.7648398518785 [[-34.452661]]\n",
      "-30.175111206359254 [[-28.351894]]\n",
      "-29.677078082263673 [[-26.562994]]\n",
      "-30.485006080625237 [[-32.547563]]\n",
      "-30.04357572414217 [[-24.94148]]\n",
      "-23.456548472992033 [[-24.762602]]\n",
      "-30.786546132438723 [[-27.859265]]\n",
      "-25.08662184147723 [[-24.025429]]\n",
      "-23.960802533557082 [[-22.863637]]\n",
      "-27.504197182528088 [[-27.154449]]\n",
      "-32.44297004189876 [[-32.710551]]\n",
      "-26.365511755022588 [[-24.14208]]\n",
      "-28.100938753805607 [[-26.033732]]\n",
      "-25.522529536226934 [[-23.284463]]\n",
      "-26.202073849390462 [[-21.250875]]\n",
      "-25.822102578307305 [[-29.64435]]\n",
      "-23.695162496522546 [[-21.146349]]\n",
      "-27.497282278776 [[-23.794467]]\n",
      "-33.692579725179606 [[-31.320567]]\n",
      "-28.73260059199701 [[-37.196242]]\n",
      "-28.32503861214822 [[-22.901027]]\n",
      "-27.933738389836197 [[-29.516081]]\n",
      "-28.091551132965456 [[-29.695863]]\n",
      "-28.476481360577928 [[-29.138859]]\n",
      "-27.627989657258876 [[-23.355301]]\n",
      "-24.087375398340775 [[-24.90467]]\n",
      "-25.981945169366494 [[-25.087982]]\n",
      "-27.962877759838708 [[-27.915039]]\n",
      "-22.084905123322276 [[-23.885224]]\n",
      "-25.718571151370654 [[-24.369527]]\n",
      "-25.631863232128726 [[-22.023272]]\n",
      "-29.026696992917927 [[-34.756981]]\n",
      "-32.26088844526508 [[-36.805424]]\n",
      "-31.80968606401924 [[-30.122588]]\n",
      "-23.96395173837431 [[-25.425166]]\n",
      "-32.08183001666215 [[-34.42282]]\n",
      "-27.62558074562819 [[-25.974489]]\n",
      "-24.454065011841454 [[-26.526482]]\n",
      "-30.558989774874572 [[-30.523149]]\n",
      "-25.559086920574863 [[-20.177964]]\n",
      "-30.07525130360068 [[-30.361714]]\n",
      "-20.132200606445814 [[-19.365356]]\n",
      "-33.919859770027024 [[-32.940343]]\n",
      "-22.585412919444558 [[-21.980284]]\n",
      "-26.71040229066139 [[-23.500859]]\n",
      "-32.8243554453594 [[-35.604958]]\n",
      "-27.54469442096279 [[-27.469822]]\n",
      "-28.089601277115257 [[-25.805073]]\n",
      "-32.18851269112778 [[-37.574153]]\n",
      "-30.16768287393391 [[-33.76968]]\n",
      "-29.03033333928309 [[-24.689869]]\n",
      "-31.645063314976923 [[-28.002767]]\n",
      "-27.924463946329567 [[-24.005383]]\n",
      "-24.707946913016354 [[-25.291998]]\n",
      "-29.29918285008617 [[-29.91651]]\n",
      "-31.353064890037214 [[-31.197088]]\n",
      "-25.044893107261085 [[-24.31428]]\n",
      "-26.53745798161764 [[-22.293879]]\n",
      "-30.79301480174212 [[-29.629092]]\n",
      "-28.118572520807973 [[-30.519177]]\n",
      "-24.05384193820358 [[-31.772269]]\n",
      "-29.73126073734153 [[-30.113308]]\n",
      "-31.84357743610936 [[-26.160162]]\n",
      "-29.551459878281577 [[-31.144393]]\n",
      "-27.737724811239108 [[-27.830412]]\n",
      "-28.584586233938772 [[-30.300173]]\n",
      "-30.346832551925456 [[-34.081622]]\n",
      "-33.5772275823203 [[-32.507768]]\n",
      "-31.828065094053493 [[-28.443651]]\n",
      "-28.12186230760987 [[-27.31189]]\n",
      "-27.487219830015075 [[-27.668376]]\n",
      "-29.469762478412257 [[-27.048838]]\n",
      "-28.277971724862315 [[-28.900829]]\n",
      "-34.991636233844325 [[-35.139714]]\n",
      "-27.78253796964996 [[-23.943962]]\n",
      "-23.525399861461093 [[-22.289861]]\n",
      "-27.59099746336227 [[-24.697077]]\n",
      "-29.36754254113381 [[-35.796166]]\n",
      "-31.13017223212644 [[-25.666207]]\n",
      "-25.939628486518945 [[-25.161309]]\n",
      "-27.5088366070026 [[-30.866572]]\n",
      "-31.110675409687374 [[-30.654283]]\n",
      "-27.40393518261161 [[-28.538393]]\n",
      "-28.84888748331814 [[-23.476155]]\n",
      "-27.867908416117025 [[-24.115886]]\n",
      "-23.483127864349605 [[-20.408436]]\n",
      "-27.112047596513367 [[-24.546129]]\n",
      "-26.47055171948493 [[-21.309363]]\n",
      "-30.864469554543433 [[-30.334568]]\n",
      "-28.39607991818133 [[-30.534206]]\n",
      "-32.42947823538009 [[-37.534605]]\n",
      "-29.32803090579175 [[-29.973863]]\n",
      "-30.954475445558224 [[-31.195785]]\n",
      "-22.971072848350108 [[-23.033678]]\n",
      "-31.164306271175477 [[-31.693342]]\n",
      "-30.298481959524334 [[-31.817053]]\n",
      "-31.702675842938614 [[-33.418762]]\n",
      "-22.34469561808118 [[-20.5956]]\n",
      "-28.49006316229958 [[-31.822448]]\n",
      "-28.97655817311646 [[-35.554906]]\n",
      "-25.47478154317883 [[-27.342615]]\n",
      "-25.350448273283128 [[-23.485852]]\n",
      "-23.97386942062839 [[-29.12834]]\n",
      "-29.323311440424042 [[-30.002598]]\n",
      "-27.104553874191765 [[-29.240391]]\n",
      "-28.80118924990029 [[-29.078613]]\n",
      "-32.76917555560073 [[-33.055663]]\n",
      "-29.281463348792222 [[-30.104794]]\n",
      "-32.242438539917316 [[-26.767678]]\n",
      "-27.9013367619279 [[-30.093786]]\n",
      "-28.0508712632909 [[-26.906383]]\n",
      "-29.6575848311227 [[-33.024109]]\n",
      "-27.332545136959524 [[-28.214158]]\n",
      "-28.213211670355264 [[-29.833389]]\n",
      "-25.682480941109787 [[-31.29836]]\n",
      "-26.638266889098713 [[-27.698624]]\n",
      "-26.379671565826317 [[-28.050526]]\n",
      "-27.903417287559876 [[-28.119836]]\n",
      "-30.346599205219796 [[-32.048623]]\n",
      "-25.182883750474247 [[-28.632448]]\n"
     ]
    }
   ],
   "source": [
    "for xi, yi in zip(x, y):\n",
    "    print(predict(xi), yi)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
